package interfaz;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.ListSelectionModel;

public class Separadores extends JDialog implements ActionListener,KeyListener {

	private Frame owner;
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JTextField jTextField = null;
	private JScrollPane jScrollPane = null;
	private JList jList = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	/**
	 * @param owner
	 */
	public Separadores(Frame owner) {
		super(owner);
		this.owner=owner;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(375, 309);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		double fil=dim.getWidth();
		double col=dim.getHeight();
		fil=(fil-375)/2;
		col=(col-309)/2;
		this.setLocation((int)fil, (int)col);
		this.setTitle("Separadores");
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		Vector<String> original=((Principal)this.owner).getSeparadores();
		this.getJList().setListData(original);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(0, 0, 375, 16));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setText("Lista de Separadores");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(47, 28, 272, 25));
			jTextField.addKeyListener(this);
		}
		return jTextField;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(47, 65, 272, 168));
			jScrollPane.setViewportView(getJList());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJList() {
		if (jList == null) {
			jList = new JList();
			jList.setBounds(new Rectangle(0, 0, 113, 168));
			jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		return jList;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(198, 246, 85, 25));
			jButton.setText("Quitar");
			jButton.addActionListener(this);
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(83, 246, 85, 25));
			jButton1.setText("Agregar");
			jButton1.addActionListener(this);
		}
		return jButton1;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand()=="Quitar")
		{
			if(this.getJList().getSelectedIndex()!=-1)
			{
				Vector<String> aux=((Principal)this.owner).getSeparadores();
				aux.removeElementAt(this.getJList().getSelectedIndex());
				((Principal)this.owner).setSeparadores(aux);
				this.getJList().setListData(aux);
			}
		}
		if(arg0.getActionCommand()=="Agregar")
		{
			if(!this.getJTextField().getText().equals(""))
			{
				Vector<String> aux=((Principal)this.owner).getSeparadores();
				aux.addElement(this.getJTextField().getText());
				((Principal)this.owner).setSeparadores(aux);
				this.getJTextField().setText("");
				this.getJList().setListData(aux);
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if(arg0.getKeyCode()==10)
		{
			if(arg0.getComponent()==this.getJTextField())
			{
				Vector<String> aux=((Principal)this.owner).getSeparadores();
				aux.addElement(this.getJTextField().getText());
				((Principal)this.owner).setSeparadores(aux);
				this.getJTextField().setText("");
				this.getJList().setListData(aux);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
